package com.example.springretrydemo.service;

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

@Service
public class TestRetryService {

    @Retryable(value = IllegalArgumentException.class, recover = "recover", maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    public int test(int code) {
        System.out.println("test() 方法被调用, 时间: " + LocalTime.now());
        if (code == 0) {
            throw new IllegalArgumentException("code 必须要大于 0!");
        }
        System.out.println("code 大于 0, test() 方法执行成功!");
        return 200;
    }

    @Recover
    public int recover(IllegalArgumentException ex, Integer code) {
        System.out.println("recover() 方法执行了");
        // 记录日志, 或者调用其余方法
        // ...
        return 400;
    }
}
